﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;

namespace ControllerClient.UserTool
{
    class UserReader
    {
        //B400000000000000004D3106F8EB0000B4000313600000000046EEDEA97B0000B300000000000000004D31094F120000B400000000000000004D310CCEED0000B300000000000000004D30EDD7690000B400000000000000004D30EE4EC80000B3000313600003135E3B20A325540000B300000000000000004D310653B700003B9B5A0E1100003801B0883E000000001032002000000000BB006854B23BDC655D96CB8885AED378BB1EBCBB233A0E30D7E70049391FE89D80A960DA5C6F2FBAA7E50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
        public static TagUserdata readTagUser(byte[] userdata)
        {
           TagUserdata tagUserdata=new TagUserdata();
           string userdatastr = StringAndBytesHelper.ToHexString(userdata, userdata.Length);
           if (userdatastr.Length != 512)
           {
               Debug.WriteLine("user区数据长度有误");
               return tagUserdata;
           }
           else
           {
               string alluserTraceDatastr = userdatastr.Substring(0, 256);//user区物流数据
               UserTraceData[] userTraceDatas = new UserTraceData[8];
               for (int i = 0; i < alluserTraceDatastr.Length / 32; i++)
               {
                   string userTraceDatastr = alluserTraceDatastr.Substring(32 * i, 32);
                   UserTraceData userTraceData = new UserTraceData();
                   userTraceData.Commandid = userTraceDatastr.Substring(0, 2);
                   userTraceData.Operator1 = userTraceDatastr.Substring(2,8);
                   userTraceData.Operator2 = userTraceDatastr.Substring(10, 8);
                   if(userTraceDatastr.Substring(18, 8).Equals("00000000"))
                   {

                        userTraceData.Opdatetime = DateTime.Parse("1900-01-01 00:00:00");
                    }else{
                        byte[] datetimeB = StringAndBytesHelper.strToToHexByte(userTraceDatastr.Substring(18, 8));
                        Array.Reverse(datetimeB);

                        Int32 datetimeint = BitConverter.ToInt32(datetimeB, 0);
                        long year = (datetimeint & 0xFC000000) >> 26;
                        //Debug.WriteLine("year===" + year);
                        long month = (datetimeint & 0x03C00000) >> 22;

                        //Debug.WriteLine("month===" + month);
                        long day = (datetimeint & 0x003E0000) >> 17;
                        //Debug.WriteLine("day===" + day);
                        long hour = (datetimeint & 0x0001F000) >> 12;
                        //Debug.WriteLine("hour===" + hour);
                        long minute = (datetimeint & 0x00000FC0) >> 6;
                        //Debug.WriteLine("minute===" + minute);
                        long second = (datetimeint & 0x0000003F);
                        //Debug.WriteLine("second===" + second);
                        //  Debug.WriteLine(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
                        userTraceData.Opdatetime = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
                    }
                   

                    userTraceDatas[i] = userTraceData;

                    Debug.WriteLine("第" + i + "条user区物流数据:命令为" + userTraceDatas[i].Commandid + " 操作人:" + userTraceDatas[i].Operator1 + " 复核人:" + userTraceDatas[i].Operator2 + " 操作时间:" + userTraceDatas[i].Opdatetime.ToString("yyyy-MM-dd HH:mm:ss:"));
               }
               tagUserdata.UserTraceData=userTraceDatas;

               string epcdatabakstr = userdatastr.Substring(256,32);//Epc备份区
               //TagEpcData tagEpcData = EpcReader.readEpc(StringAndBytesHelper.strToToHexByte(epcdatabakstr.Substring(0,24)));//只取前12byte作为EPC
              // tagUserdata.TagEpcDatabak=tagEpcData;


               Debug.WriteLine("硬件版本为:" + userdatastr.Substring(288, 1));//硬件版本
               Debug.WriteLine("软件版本为:" + userdatastr.Substring(289, 3));//软件版本
               Debug.WriteLine("日志计数为:" + userdatastr.Substring(292, 4));//日志计数

               string errordatastr = userdatastr.Substring(296, 8);//异常标志位
               BitArray errorBA = new BitArray(StringAndBytesHelper.strToToHexByte(errordatastr));
              // Boolean[] Errors = new Boolean[errorBA.Length];
               tagUserdata.UserErrorData = new Boolean[errorBA.Length];
               for (int i = 0; i < errorBA.Length; i++)
               {
                   Debug.WriteLine(errorBA[i] == false ? "异常置位"+i+":false" : "异常置位" + i + ":true");
                   tagUserdata.UserErrorData[i] = errorBA[i];
               }
               //tagUserdata.UserErrorData.Errors = Errors;
               return tagUserdata;
           }
        } 
    }
}
